<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var longestStrChain = function (words) {
      words.sort((a, b) => a.length - b.length);
      const M = new Map();
      let res = 1;
      for (let s of words) {
        if (M.has(s)) continue;  // 重复
        M.set(s, 1);
        for (let i = 0; i < s.length; i++) {
          if (i > 0 && s[i] === s[i - 1]) continue;  // 重复
          let t = s.substr(0, i) + s.substr(i + 1);
          if (M.has(t) && M.get(t) >= M.get(s)) {
            M.set(s, M.get(t) + 1);
          }
        }
        res = Math.max(res, M.get(s));
      }
      return res;
    };
    console.log(longestStrChain(["a", "b", "ba", "bca", "bda", "bdca"]))
  </script>
</body>

</html>